package all.com.jinyongqunxiax.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.randy.fundation.Logger;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.component.JxForm;
import all.com.jinyongqunxiax.component.JxFormHeader;
import all.com.jinyongqunxiax.component.JxFormItemImage;
import all.com.jinyongqunxiax.component.JxFormItemSwitch;
import all.com.jinyongqunxiax.component.JxFormItemText;
import all.com.jinyongqunxiax.databinding.RoleDetailActivityBinding;
import all.com.jinyongqunxiax.model.mod.RoleManager;
import all.com.jinyongqunxiax.model.mod.manager.Mod;
import all.com.jinyongqunxiax.model.mod.manager.ModManager;
import all.com.jinyongqunxiax.view.JxHud;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import jy.game.components.item.JyItemInstance;
import jy.game.components.role.JyRole;
import jy.game.components.wuxue.box.JyInternalSkillInstance;
import jy.game.components.wuxue.box.JySkillInstance;
import jy.game.components.wuxue.box.JySpecialSkillInstance;

public class RoleDetailActivity extends AppCompatActivity {
    private Options                   options;
    private RoleDetailActivityBinding binding;
    JxForm form = new JxForm();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        options = new Options(getIntent());

        setTitle(options.item);

        Mod         mod     = ModManager.shared().get(options.mod);
        RoleManager manager = mod.roleManager;
        JyRole      model   = manager.map.get(options.item);

        binding = DataBindingUtil.setContentView(this, R.layout.role_detail_activity);

        RecyclerView recyclerView = binding.recyclerView;
        form.connect(this, recyclerView);

        if (model != null) {
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("名称").value(model.name).hint("").value(), null));
            String imagePath = mod.imagePath(model.head);
            form.add(new JxFormItemImage(0, "头像", imagePath));

            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("key").value(model.key).hint("").value(), null));
            form.add(new JxFormItemSwitch(0, "女性", model.femaleValue == 0, null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("等级").value("" + model.level).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("成长").value(model.growTemplateValue).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("竞技场").value(model.arenaValue).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("动画资源").value(model.animation).hint("").value(), null));

            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("武学").value("" + model.wuxue).hint("").value(), null));

            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("内力上限").value("" + model.maxmp).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("生命上限").value("" + model.maxhp).hint("").value(), null));

            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("拳掌").value("" + model.quanzhang).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("剑法").value("" + model.jianfa).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("刀法").value("" + model.daofa).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("奇门").value("" + model.qimen).hint("").value(), null));

            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("臂力").value("" + model.bili).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("定力").value("" + model.dingli).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("福源").value("" + model.fuyuan).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("根骨").value("" + model.gengu).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("身法").value("" + model.shenfa).hint("").value(), null));
            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("悟性").value("" + model.wuxing).hint("").value(), null));

            if (model.talentValue != null) {
                form.add(new JxFormHeader(0, "天赋", null));
                String[] talents = model.talentValue.split("#");
                for (String talent : talents) {
                    if (talent.trim().isEmpty()) {
                        continue;
                    }
                    form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(talent).value("").hint("").value(), null));
                }
            }

            if (model.skills != null) {
                form.add(new JxFormHeader(0, "外功", null));
                for (JySkillInstance skill : model.skills) {
                    form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(skill.name).value("" + skill.level).hint("").value(), null));
                }
            }

            if (model.internalSkills != null) {
                form.add(new JxFormHeader(0, "内功", null));
                for (JyInternalSkillInstance skill : model.internalSkills) {
                    form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(skill.name).value("" + skill.level).hint("").value(), null));
                }
            }

            if (model.specialSkills != null) {
                form.add(new JxFormHeader(0, "绝技", null));
                for (JySpecialSkillInstance skill : model.specialSkills) {
                    form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(skill.name).value("").hint("").value(), null));
                }
            }

            if (model.equipment != null) {
                form.add(new JxFormHeader(0, "物品", null));
                for (JyItemInstance item : model.equipment) {
                    form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(item.name).value("1").hint("").value(), null));
                }
            }
        }
        form.refresh();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.plus_button_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_button:
                JxHud.info("add menu clicked");
                break;
            default:
                Logger.w("not handle menu %d", item.getItemId());
                break;
        }
        return true;
    }

    public static class Options {
        String mod;
        String item;

        Options(Intent intent) {
            this.mod = intent.getStringExtra("mod");
            this.item = intent.getStringExtra("item");
        }

        static Intent intent(Context context, String mod, String item) {
            Intent intent = new Intent(context, RoleDetailActivity.class);
            intent.putExtra("mod", mod);
            intent.putExtra("item", item);
            return intent;
        }
    }
}
